/**
 * Created by Administrator on 2016/3/2.
 */
gpNamespace.YjLayer = cc.Layer.extend({
	mailList: null,
	mailDetail: null,
	cell: null,
	contentArray: null,
	leftMenu: null,

	ctor: function () {
        this._super();
        this.init();
    },
    init: function () {
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkyjRes.Pdkyj_plist);
        var size = cc.winSize;

		//背景图层
		var background = new cc.Sprite(gpNamespace.yj.Yj_Bg);
		background.attr({
			x: 108  + background.width / 2,
			y: 30 + background.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(background);

		//左边滚动条
		/*var leftScrollBar = new cc.Sprite(gpNamespace.yj.Yj_LScrollBar);
        leftScrollBar.attr({
            x: 132  + leftScrollBar.width / 2,
            y: 202 + leftScrollBar.height / 2,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(leftScrollBar);*/

		//邮件列表 
		var mailList = new tableViewPro.TableView(this, cc.size(475, 300), 0);
		mailList.setPosition(155, 210);
		mailList.setAnchorPoint(0, 0);
		this.mailList = mailList;
		this.addChild(mailList);
		mailList.setTouchEnabled(false);
		mailList.setBarOffset(0, 0);

		//左边清空按钮
		var msgClearBt=new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.yj.Yj_ClearMsg),
				new tyNamespace.maskSprite(gpNamespace.yj.Yj_ClearMsg),
				this.clearCallbackFunction, this);
		
		//收取全部
		var collectAll = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.yj.Yj_CollectAll),
				new tyNamespace.maskSprite(gpNamespace.yj.Yj_CollectAll),
				this.collectAllCallbackFunction, this);

		var leftMenu = new cc.Menu(msgClearBt, collectAll);

		leftMenu.attr({
			x: 192+(msgClearBt.width+collectAll.width+74)/2,
			y: 122+msgClearBt.height/2,
		});
		leftMenu.alignItemsHorizontallyWithPadding(74);
		this.leftMenu = leftMenu;
		this.addChild(leftMenu,2,5);

		//右侧文字背景
		var rightMsgBg = new cc.Sprite(gpNamespace.yj.Yj_MsgBg);
		rightMsgBg.attr({
			x: 660 + rightMsgBg.width / 2,
			y: 114 + rightMsgBg.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(rightMsgBg);

		//邮件详情
		var mailDetail = new tableViewPro.TableView(this, cc.size(420, 300), 0);
		mailDetail.setPosition(688, 154);
		mailDetail.setAnchorPoint(0, 0);
		this.mailDetail = mailDetail;
		this.addChild(mailDetail);
		mailDetail.setTouchEnabled(false);
		mailDetail.setBarOffset(0, 0);

		//右侧滚动条
		/*var rightScrollBar = new cc.Sprite(gpNamespace.yj.Yj_RScrollBar);
        rightScrollBar.attr({
            x: 1072  + rightScrollBar.width / 2,
            y: 200 + rightScrollBar.height / 2,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(rightScrollBar);*/


		gpNamespace.TyNetManager.getInstance().setMailLayer(this);//将本页面信息传给netManager
		xinyou.netManager.getMail(1);//获取邮件
	},
//	显示左侧邮件列表
	setMailContainer:function (data) {
		this.mailList.clearData();
		this.mailList.addData({
			"barInfo": {"width": 2, "height": 80},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 1},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": data.itemCount,
			            		 "imageName": gpNamespace.yj.Yj_NewsNotOpenBg,
			            		 "height": 110
			            	 }
			             }
			             ]
		});
		this.mailList.showData();
		this.mailList.setTouchEnabled(true);

		var cellCount = this.mailList.currentItem.cellInfo.cellCount;
		var cellType = this.mailList.currentItem.cellInfo.cellType;
		var currentPage = this.mailList.currentPage;
//		cc.log("*******"+cellCount+">>"+cellType);
		for (var a = 0; a < cellCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
				/*case 1://menu
    			cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
    			break;*/
			}
			this.setMailCell(a, cell, data);
		}
	},

	setMailCell: function (index, cell, data) { 
		var exchangeItems = [];
		var newsBg = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.yj.Yj_NewsNotOpenBg),
				new tyNamespace.maskSprite(gpNamespace.yj.Yj_NewsNotOpenBg),
				this.mailCallbackFunction, this);
		newsBg.attr({
			x: 0,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		newsBg.setUserData(data.mails[index][2]);//邮件ID
		exchangeItems.push(newsBg);

		/*判断邮件是否有附件*/
		//先放一个已经打开的图标在下面
		var hasread = new cc.Sprite(gpNamespace.yj.Yj_NewsOpenedWhitePic);
		hasread.attr({
			x: 25,
			y: 55,
			anchorX: 0,
			anchorY: 0.5
		});
		hasread.visible = false;
		hasread.setName("mailOpened");
		cell.addChild(hasread,3);

		var path = gpNamespace.yj.Yj_NewsSysNotOpenYellowPic;
		if(data.mails[index][1]==0 && data.mails[index][3] == 2){
			hasread.visible = false;
			path = gpNamespace.yj.Yj_NewsSysNotOpenYellowPic;
//			cc.log("未打开 系统");
			var isread = new cc.Sprite(path);
			isread.attr({
				x: 25,
				y: 50,
				anchorX: 0,
				anchorY: 0.5
			});
			isread.setName("mailNotOpen");
			cell.addChild(isread,3);
		}else if(data.mails[index][1]==0 && data.mails[index][3] == 1){
			hasread.visible = false;
			path = gpNamespace.yj.Yj_NewsNotOpenWhitePic;
//			cc.log("未打开 好友");
			var isread = new cc.Sprite(path);
			isread.attr({
				x: 25,
				y: 50,
				anchorX: 0,
				anchorY: 0.5
			});
			isread.setName("mailNotOpen");
			cell.addChild(isread,3);
		}else{
			hasread.visible = true;
//			cc.log("已打开");
		}
		/*var isread = null;
    	if(data.mails[index][0]==1){
    		//判断邮件是否已读  0:未读， 1：已读但未收取， 2：已收取（附件）
    		if(data.mails[index][1]==0 || data.mails[index][1]==1){
    			hasread.visible = false;
    			//未读
    			isread = new cc.Sprite(gpNamespace.yj.Yj_NewsSysNotOpenYellowPic);
    			isread.attr({
    				x: 25,
    				y: 50,
    				anchorX: 0,
    				anchorY: 0.5
    			});
    			isread.setName("mailNotOpen");
    			cell.addChild(isread,3);
    		}else{//有附件已接收
    			hasread.visible = true;
    		}
    	}else{
    		if(data.mails[index][1]==0){
    			hasread.visible = false;
    			//未读
    			isread = new cc.Sprite(gpNamespace.yj.Yj_NewsNotOpenWhitePic);
    			isread.attr({
    				x: 25,
    				y: 50,
    				anchorX: 0,
    				anchorY: 0.5
    			});
    			isread.setName("mailNotOpen");
    			cell.addChild(isread,3);
    		}else{//已读
    			hasread.visible = true;
    		}
    	}*/

		//邮件标题
		var title = new cc.LabelTTF(data.mails[index][6], gpNamespace.DEFAULTFONT, 30);
		title.setFontFillColor(cc.color(108, 28, 11, 255));
		title.attr({
			x: 110,
			y: 65,
			anchorX: 0,
			anchorY: 0.5
		});
		cell.addChild(title,3);

		//邮件类型
		var mailType = "系统消息";
		/*if(data.mails[index][3]==2){
    		mailType = "系统消息"
    	}else */if(data.mails[index][3]==1){
    		mailType = "好友消息"
    	}
    	var type = new cc.LabelTTF(mailType, gpNamespace.DEFAULTFONT, 20);
    	type.setFontFillColor(cc.color(108, 28, 11, 255));
    	type.attr({
    		x: 110,
    		y: 30,
    		anchorX: 0,
    		anchorY: 0.5
    	});
    	cell.addChild(type,3);

    	//邮件日期时间
    	var date = new cc.LabelTTF(data.mails[index][4], gpNamespace.DEFAULTFONT, 20);
    	date.setFontFillColor(cc.color(108, 28, 11, 255));
    	date.attr({
    		x: 250,
    		y: 30,
    		anchorX: 0,
    		anchorY: 0.5
    	});
    	cell.addChild(date,3);

    	//删除
    	var del = new cc.MenuItemSprite(
    			new cc.Sprite(gpNamespace.yj.Yj_Del),
    			new tyNamespace.maskSprite(gpNamespace.yj.Yj_Del),
    			this.delCallbackFunction, this);
    	del.attr({
    		x: 360,
    		y: 45,
    		anchorX: 0,
    		anchorY: 0.5
    	});
    	del.setUserData(data.mails[index][2]);
    	exchangeItems.push(del);

    	var newsMenu = new tableViewPro.Menu(exchangeItems);
    	newsMenu.attr({
    		x: 0,
    		y: 0,
    		anchorX: 0,
    		anchorY: 0
    	});
    	cell.addChild(newsMenu);
//  	newsMenu.setMyParent(this.mailList);
    	newsMenu.setTouchListener();
    	this.scheduleOnce(function () {
    		newsMenu.setTouchArea(this.mailList.clippingNode.getContentSize(), this.mailList.clippingNode.getParent().convertToWorldSpace(this.mailList.clippingNode.getPosition()));
    	}, 0.5);
	},
	mailCallbackFunction:function(sender){
		var mailId = sender.getUserData();
//		console.log("MailCallbackFunction:"+mailId);
		xinyou.netManager.getMailInfo(mailId);//获取单个邮件
		var isNull = null;
		if(isNull=sender.getParent().getParent().getChildByName("mailNotOpen")){
			//这里的sender是newsBg本身，第一个getParent()获取到newsBgde爹，它爹并不是它所在的cell，而是它所在的menu，它爷爷才是cell！！！
			sender.getParent().getParent().getChildByName("mailNotOpen").visible = false;
			sender.getParent().getParent().getChildByName("mailOpened").visible = true;
		}
	},
	delCallbackFunction:function(sender){
		tyNamespace.setMenuEnable(sender);
		var mailId = sender.getUserData();
		console.log("delCallbackFunction");
		var self = this; 
		var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer(70);
		modelLayer.setTouchArea(cc.size(570, 345), cc.p(350, 200));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 10);
		var scJumpLayer = new tyNamespace.TipsLayer(2,tyNamespace.TipsSectencesIndex.DelOneEmail,function(){
			xinyou.netManager.mailEdit(3,mailId);//删除单个邮件
			self.mailDetail.clearData();
			self.mailList.clearData();
			xinyou.netManager.getMail(1);//获取邮件
		});
		scJumpLayer.attr({
			//左上角坐标
			x: 0,
			y: cc.winSize.height,
		});
		modelLayer.addChild(scJumpLayer);
		tyNamespace.AnimationManager.nodeJumpShow(scJumpLayer, -cc.winSize.height);
	},
	clearCallbackFunction:function(sender){
		tyNamespace.setMenuEnable(sender);
		var mailId = sender.getUserData();
		var self = this; 
		var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer(70);
		modelLayer.setTouchArea(cc.size(570, 345), cc.p(350, 200));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 10);
		var scJumpLayer = new tyNamespace.TipsLayer(2,tyNamespace.TipsSectencesIndex.DelOneEmail,function(){
			xinyou.netManager.mailEdit(4,mailId);//删除所有邮件
			self.mailDetail.clearData();
			self.mailList.clearData();
			xinyou.netManager.getMail(1);//获取邮件
//			self.setNoMail();
		});
		scJumpLayer.attr({
			//左上角坐标
			x: 0,
			y: cc.winSize.height,
		});
		modelLayer.addChild(scJumpLayer);
		tyNamespace.AnimationManager.nodeJumpShow(scJumpLayer, -cc.winSize.height);
	},
	collectAllCallbackFunction:function(sender){
		tyNamespace.setMenuEnable(sender);
		var mailId = sender.getUserData();
		var self = this; 
		var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer(70);
		modelLayer.setTouchArea(cc.size(570, 345), cc.p(350, 200));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 10);
		var scJumpLayer = new tyNamespace.TipsLayer(2,tyNamespace.TipsSectencesIndex.GetAllAttachmentSuc,function(){
			xinyou.netManager.mailEdit(2,mailId);//领取所有邮件
			xinyou.netManager.getMail(1);//获取邮件
			self.mailDetail.clearData();
		});
		scJumpLayer.attr({
			//左上角坐标
			x: 0,
			y: cc.winSize.height,
		});
		modelLayer.addChild(scJumpLayer);
		tyNamespace.AnimationManager.nodeJumpShow(scJumpLayer, -cc.winSize.height);
	},
	//显示单个邮件详情
	setMailDetail:function(mailInfo){ 
//		this.contentArray = [];
		tyNamespace.SlicedString = [];//坑,控件中用了递归，并且第一次以后调用没有将之前的数组清空。导致结果累计
		this.contentArray = tyNamespace.GetSlicedString("      "+mailInfo.content,30);
		var cellheight = this.contentArray.length * 40;
		this.cellheight = cellheight;
		cc.log("height:"+this.cellheight);
		this.mailDetail.clearData();
		this.mailDetail.addData({
			"barInfo": {"width": 2, "height": 80},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 1},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": 1,
			            		 "imageName": "",
			            		 "height": cellheight
			            	 }
			             }
			             ]
		});
		this.mailDetail.showData();
		this.mailDetail.setTouchEnabled(true);

		var cellCount = this.mailDetail.currentItem.cellInfo.cellCount;
		var cellType = this.mailDetail.currentItem.cellInfo.cellType;
		var currentPage = this.mailDetail.currentPage;
		for (var a = 0; a < cellCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
			cell = currentPage.getChildByTag(a);
//			cell.addChild(this.node);
//			cell.removeAllChildren();
			break;
			/*case 1://menu
    			cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
    			break;*/
			}
			this.setMailDetailCell(a, cell, mailInfo);
		}
	},
	setMailDetailCell:function(index, cell, oneMail){
//		var node = cell.getChildByName("mailnode");
//		console.log("1111111111"+node.getName());
		//标题
		var title = new cc.LabelTTF("亲爱的玩家你好:",gpNamespace.DEFAULTFONT,30);
		title.setPosition(20, cell.height-25);
		title.anchorX = 0;
		title.anchorY = 0.5;
		cell.addChild(title);

		//内容
		var contentArray = [];
		contentArray = this.contentArray;
//		cc.log(contentArray);
		var content = new cc.Sprite();
		var dateHeight = 50;
		for(var i=0; i<contentArray.length; i++){
			//一行内容
			var oneline = new cc.LabelTTF(contentArray[i],gpNamespace.DEFAULTFONT,25);
			dateHeight = cell.height-60-30*i
			oneline.setPosition(5, dateHeight);
			oneline.anchorX = 0;
			oneline.anchorY = 0.5;
			content.addChild(oneline);
		}
		cell.addChild(content);
		//邮件日期时间
		var date = new cc.LabelTTF("日期:"+oneMail.createDate, gpNamespace.DEFAULTFONT, 25);
		date.attr({
			x: cell.width - 10,
			y: dateHeight - 25,
			anchorX: 1,
			anchorY: 1
		});
		cell.addChild(date,3);
		if(oneMail.attachmentCount > 0){
//			cc.log("有附件按钮");
			var collectBt = new cc.MenuItemSprite(
					new cc.Sprite(gpNamespace.yj.Yj_CollectAccessory),
					new tyNamespace.maskSprite(gpNamespace.yj.Yj_CollectAccessory),
					function(){
						var mailId = oneMail.mailID;
						xinyou.netManager.mailEdit(1,mailId);//领取单个邮件
						collectBt.visible = false;
					},
					this); 
			collectBt.attr({
				x: cell.width/2 - 20,
				y: dateHeight - 25,
				anchorX: 1,
				anchorY: 1
			});
			var collectMenu = new cc.Menu(collectBt);
			collectMenu.attr({
				x: 0,
				y: 0,
				anchorX:0,
				anchorY:0
			});
			cell.addChild(collectMenu,3);
		}
	},
	setNoMail:function(){
		//this.leftMenu.setEnabled(false);
		var noMail = new cc.Sprite(gpNamespace.yj.Yj_NoMail);
		noMail.attr({
			x:400,
			y:350
		});
		this.addChild(noMail);
	},
	sethiddle:function(){
		this.removeChildByTag(5);
	},
	onExit:function(){
		this._super();
		gpNamespace.TyNetManager.getInstance().setMailLayer(null);//将本页面信息传给netManager
		xinyou.netManager.getMail(1);
//		cc.log("邮件onExit");
	}
});